
import { changeStatus, userList, usernameList } from '@/services';
import { IUserList } from '@/types';
import { Effect, Reducer } from 'umi';

export interface UserModelState {
  userList: IUserList [],
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    userList: Effect;
    changeStatus:Effect;
    usernameList:Effect
  };
  reducers: {
    save: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',
  state: {
    userList: [],
  },

  effects: {
    *userList({payload}, {call, put}){
      let result = yield call(userList);
      if (result.data){
        yield put({
          type: 'save',
          payload: {
            userList: result.data[0],
          }
        })
      }
    },
    *changeStatus({ payload }, { call , put}) {
      let result = yield changeStatus(payload);
      if(result.data){
        console.log('------------------------------',payload); 
        yield put({
          type: 'save',
          payload
        })
      }
    },
    *usernameList({payload}, {call, put}){
      let result = yield usernameList(payload);
      
      if (result.data){
        yield put({
          type: 'save',
          payload: {
            userList: result.data[0],
          }
        })
      console.log(userList,'------------------------------');
  
      }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default UserModel;
